<div class="ui basic segment">
  <h1 class="ui dividing header">
    <%= link("Podcasts", to: ~p"/admin/podcasts") %>
    <%= render(PodcastView, "_dropdown_list.html", assigns) %>
    <%= link(@podcast.name, to: ~p"/admin/podcasts/#{@podcast.slug}/episodes") %>
    &raquo;
    <%= link("Episode Requests", to: ~p"/admin/podcasts/#{@podcast.slug}/episode_requests") %>
    &raquo;
    <%= @request.id %> <%= status_label(@request) %>

  <%= if Policies.Admin.EpisodeRequest.edit(@current_user, @podcast) do %>
    <%= link("(Edit)", to: ~p"/admin/podcasts/#{@podcast.slug}/episode_requests/#{@request}/edit", class: "view") %>
  <% end %>
  </h1>
</div>

<div class="ui basic segment">
  <div class="ui text container">
  <%= if @request.episode do %>
    <h4 class="ui block header">Resulting Episode</h4>

    <p><%= link(EpisodeView.numbered_title(@request.episode), to: ~p"/admin/podcasts/#{@podcast.slug}/episodes/#{@request.episode.slug}/edit") %></p>
  <% end %>

  <div class="actions right">
    <div class="ui basic buttons">
    <%= if Changelog.EpisodeRequest.is_undecided(@request) && Policies.Admin.Episode.create(@current_user, @podcast) do %>
      <%= AdminHelpers.icon_link("plus", to: ~p"/admin/podcasts/#{@podcast.slug}/episodes/new?request_id=#{@request.id}", title: "Start Episode") %>
    <% end %>

    <%= if Changelog.EpisodeRequest.is_pendable(@request) && Policies.Admin.EpisodeRequest.pend(@current_user, @podcast) do %>
      <%= AdminHelpers.icon_link("hourglass half", to: ~p"/admin/podcasts/#{@podcast.slug}/episode_requests/#{@request}/pend", method: :put, title: "Set to Pending") %>
    <% end %>

    <%= if Changelog.EpisodeRequest.is_undecided(@request) && Policies.Admin.EpisodeRequest.decline(@current_user, @podcast) do %>
      <%= AdminHelpers.icon_link("thumbs down",
          to: ~p"/admin/podcasts/#{@podcast.slug}/episode_requests/#{@request}/decline",
          title: "Decline Silently",
          method: :put,
          data: [confirm: "Decline this request?"]) %>

      <%= AdminHelpers.modal_icon_button(
        ChangelogWeb.Admin.EpisodeRequestView,
        "bullhorn",
        "Decline with Message",
        "decline",
        %{request: @request, conn: @conn, podcast: @podcast},
        @request.id
      ) %>
    <% end %>

    <%= if Changelog.EpisodeRequest.is_undecided(@request) && Policies.Admin.EpisodeRequest.fail(@current_user, @podcast) do %>
      <%= AdminHelpers.icon_link("frown",
          to: ~p"/admin/podcasts/#{@podcast.slug}/episode_requests/#{@request}/fail?next=#{SharedHelpers.current_path(@conn)}",
          title: "Fail Silently",
          method: :put, data: [confirm: "Set to failed?"]) %>

      <%= AdminHelpers.modal_icon_button(
        ChangelogWeb.Admin.EpisodeRequestView,
        "bullhorn",
        "Fail with Message",
        "fail",
        %{request: @request, conn: @conn, podcast: @podcast},
        @request.id
      ) %>
    <% end %>

    <%= if !Changelog.EpisodeRequest.is_complete(@request) && Policies.Admin.EpisodeRequest.delete(@current_user, @podcast) do %>
      <%= AdminHelpers.icon_link("trash",
        to: ~p"/admin/podcasts/#{@podcast.slug}/episode_requests/#{@request}",
        method: :delete, title: "Delete", data: [confirm: "Delete this request?"]) %>
    <% end %>
    </div>
  </div>

  <%= if @request.status == :declined and @request.message do %>
    <h4 class="ui block header">Declined with message</h4>

    <p><%= @request.message |> SharedHelpers.md_to_html() |> raw() %></p>
  <% end %>

  <%= if @request.status == :failed and @request.message do %>
    <h4 class="ui block header">Failed with message</h4>

    <p><%= @request.message |> SharedHelpers.md_to_html() |> raw() %></p>
  <% end %>

  <h4 class="ui block header">Submitted By</h4>

  <p>
  <%= if Policies.Admin.Person.show(@current_user, @request.submitter) do %>
    <%= link(@request.submitter.name, to: ~p"/admin/people/#{@request.submitter}") %>
  <% else %>
    <%= @request.submitter.name %>
  <% end %>
    (<a href="mailto:<%= @request.submitter.email %>">contact via email</a>) on <%= AdminHelpers.ts(@request.inserted_at) %></p>

  <h4 class="ui block header">Topic and/or Guests</h4>

  <p><%= @request.topics || "None specified" %> <strong>with</strong> <%= @request.guests || "None specified" %></p>

  <h4 class="ui block header">Pitch</h4>

  <p><%= @request.pitch |> SharedHelpers.md_to_html() |> raw() %></p>

  <h4 class="ui block header">Requested Hosts</h4>

  <p><%= @request.hosts || "None specified" %></p>

  <h4 class="ui block header">On-air Credit</h4>

  <p><%= credit(@request) %></p>
  </div>
</div>
